﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;

using Wicresoft.Framework.Organization;
using Wicresoft.JDPostBoard.Entity;
using Wicresoft.Busi.BizLogic;
using Wicresoft.Busi.Entity;

public partial class Car_Message : BaseAuthenticationPage
{
    #region Property ApplyID
    public int ApplyID
    {
        get
        {
            return int.Parse(Request["ApplyID"]);
        }
    }
    #endregion

    public string UserType
    {
        get
        {
            return Request["UserType"];
        }
    }

    public string Source
    {
        get
        {
            return Request["Source"];
        }
    }

    private string ModuleID
    {
        get
        {
            return string.Format(@"<?xml version=""1.0"" encoding=""UTF-8""?><value type=""System.Guid"">{0}</value>", ConfigurationManager.AppSettings["ModuleID"]);
        }
    }
    private string SendTime
    {
        get
        {
            return string.Format(@"<?xml version=""1.0"" encoding=""UTF-8""?><value type=""System.DateTime"">{0}</value>", DateTime.Now);
        }
    }
    private string SenderName
    {
        get
        {
            return CurrentUser.DisplayName;
        }
    }


    public string getReceiver(string userName, string userDisplayName, string Mobile)
    {
        return string.Format(@"<?xml version=""1.0"" encoding=""UTF-8""?>
                                    <Receivers>
  			                                   <Receiver>
    				                                    <Name>{0}</Name>
    				                                    <DisplayName>{1}</DisplayName>
    				                                    <NodeType>User</NodeType>
    				                                    <Email></Email>
    				                                    <LCS></LCS>
    				                                    <Mobile>{2}</Mobile>
    				                                    <Fax></Fax>
  			                                    </Receiver>
                                    </Receivers>", userName, userDisplayName, Mobile);
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            int vehicleid = 0;

            string msg = "";
            string Applicant = "";
            if (Source == "Car" && UserType == "Modify")
            {
                Wicresoft.JDPostBoard.Entity.VehicleApply2 info = new Wicresoft.JDPostBoard.Entity.VehicleApply2(this.ApplyID);
                info.Load();
                Applicant = info.Applicant;
                vehicleid = info.VehicleID;
                txtMobile.Text = info.Phone;
                string vehicleNumber = string.Empty;
                string lxfsnumber = string.Empty;
                if (vehicleid == 999)
                {
                    lxfsnumber = info.Lxfs;
                   
                    vehicleNumber = info.Ch;
                }
                else
                {
                    Vehicles ve = new Vehicles(vehicleid);
                    if (ve.Load())
                    {
                        lxfsnumber = ve.DriverPhone; 
                        vehicleNumber = ve.VehicleNo;
                    }
                }
                msg = "您{0}预约的用车已更改,车牌为{1},联系电话为:{2}";
                this.txtContent.Text = string.Format(msg, info.ApplyDate.ToString("MM月dd日"), vehicleNumber, lxfsnumber);
            }
            else if (Source == "Car" && UserType == "Cancel")
            {
                Wicresoft.JDPostBoard.Entity.VehicleApply2 info = new Wicresoft.JDPostBoard.Entity.VehicleApply2(this.ApplyID);
                info.Load();
                txtMobile.Text = info.Phone;
                Applicant = info.Applicant;
                msg = "您的申请现已取消：{0}于{1}至{2}日申请使用公车。";
                this.txtContent.Text = string.Format(msg, info.ApplicantName, info.StartDate.ToString("MM月dd日 HH时mm分"), info.EndDate.ToString("MM月dd日 HH时mm分"));
            }
            else if (Source == "Car" && UserType == "Driver")
            {
                Wicresoft.JDPostBoard.Entity.VehicleApply2 info = new Wicresoft.JDPostBoard.Entity.VehicleApply2(this.ApplyID);
                info.Load();
                Applicant = info.Applicant;
                vehicleid = info.VehicleID;
               string vehicleNumber = string.Empty;
                if (vehicleid == 999)
                {
                    txtMobile.Text = info.Lxfs;
                    vehicleNumber = info.Ch;
                }
                else
                {
                    Vehicles ve = new Vehicles(vehicleid);
                    if (ve.Load())
                    {
                        txtMobile.Text = ve.DriverPhone;
                        vehicleNumber = ve.VehicleNo;
                    }
                }
                msg = "{0}车辆{1}预约已更改,预约时间为{2}至{3}, 预约人{4},电话:{5}";
                this.txtContent.Text = string.Format(msg, info.ApplyDate.ToString("MM月dd日"),vehicleNumber, info.StartDate.ToString("MM月dd日 HH时mm分"), info.EndDate.ToString("MM月dd日 HH时mm分"),info.ApplicantName,info.Phone);


            }
            else if (Source == "Room")
            {
                vwMeetingRoomApply2 info = new vwMeetingRoomApply2();
                info.Load("ID='" + this.ApplyID + "'");

                Applicant = info.Applicant;
                if (info.Status == Wicresoft.Busi.Entity.MeetingStatus.AUDIT)
                {
                    msg = "您的申请现已变更为：{0}申请{2}使用{3}。";
                    msg = string.Format(msg, info.ApplicantName, info.Topic, info.MeetingDate.ToString("MM月dd日") + " " + info.StartTime + "-" + info.EndTime, info.RoomName);
                }
                else if (info.Status == Wicresoft.Busi.Entity.MeetingStatus.CANCEL)
                {
                    msg = "您的申请现已取消：{0}应{1}会议申请于{2}使用{3}。";
                    msg = string.Format(msg, info.ApplicantName, info.Topic, info.MeetingDate.ToString("MM月dd日") + " " + info.StartTime + "-" + info.EndTime, info.RoomName);
                }
                this.txtContent.Text = msg;
            }
            if (Source == "Room")
            {
                User User = OrganizationHelper.GetUserInfo(Applicant);
                if (User != null)
                {
                    DataTable dtData = Wicresoft.Common.DataAccess.ExecuteSql("Wicresoft.Framework.ConnectionString",
                     string.Format(@"Select ValueString from PropertyData where NodeID='{0}' and PropertyID in (
                    select ID from Property where Name='Mobile')", User.ID.ToString())).Tables[0];

                    LogHelper.WriteLog("申请人：" + Applicant + ",手机号获取:" + string.Format(@"Select ValueString from PropertyData where NodeID='{0}' and PropertyID in (
                    select ID from Property where Name='Mobile')", User.ID.ToString()));

                    string mobile = dtData.Rows.Count == 0 ? "" : dtData.Rows[0]["ValueString"].ToString();
                    if (!string.IsNullOrEmpty(mobile))
                        this.txtMobile.Text = mobile;

                }
            }

        }

    }
    protected void btnConfirm_Click(object sender, EventArgs e)
    {
        if (this.txtMobile.Text.Trim() == "")
        {
            WebCommon.ShowMessage(this.Page, "请输入手机号.");
            this.txtMobile.Focus();
            return;
        }
        if (this.txtContent.Text.Trim() == "")
        {
            WebCommon.ShowMessage(this.Page, "请输入短信内容.");
            this.txtContent.Focus();
            return;
        }
        if (Request["Source"] == "Car")
        {
            Wicresoft.JDPostBoard.Entity.VehicleApply2 info = new Wicresoft.JDPostBoard.Entity.VehicleApply2(this.ApplyID);
            if (info.Load())
            {
                User User = OrganizationHelper.GetUserInfo(info.Applicant);

                MessageService.MessagesService service = new MessageService.MessagesService();
                string status = service.SendMessageByModuleID(ModuleID, SenderName, getReceiver(User.Name, User.DisplayName, this.txtMobile.Text), this.txtContent.Text, "", false, 1, string.Empty, SendTime);
            }
        }
        if (Request["Source"] == "Room")
        {
            Wicresoft.JDPostBoard.Entity.MeetingRoomApply info = new Wicresoft.JDPostBoard.Entity.MeetingRoomApply(this.ApplyID);
            if (info.Load())
            {
                User User = OrganizationHelper.GetUserInfo(info.Applicant);

                MessageService.MessagesService service = new MessageService.MessagesService();
                string status = service.SendMessageByModuleID(ModuleID, SenderName, getReceiver(User.Name, User.DisplayName, this.txtMobile.Text), this.txtContent.Text, "", false, 1, string.Empty, SendTime);
            }
        }


        
        WebCommon.ShowMessageAndClose(this.Page, "已发送短信.");
    }
}